package cn.fintecher.pangolin.service.dataimp.request;

import cn.fintecher.pangolin.common.enums.ImportTemplateType;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

import java.util.Objects;

/**
 * @Auther: xiaqun
 * @Description: 导入模板查询条件
 * @Date: 10:51 2019/8/24
 */

@Data
public class FindImportTemplateRequest {
    @ApiModelProperty(value = "模板名称")
    private String name;

    @ApiModelProperty(value = "委托方ID")
    private String principalId;

    @ApiModelProperty(value = "模板类型")
    private ImportTemplateType type;

    public BoolQueryBuilder getBuilder() {
        BoolQueryBuilder builder = new BoolQueryBuilder();
        if (StringUtils.isNotBlank(this.name)) {
            builder.must(QueryBuilders.wildcardQuery("name.keyword", "*" + StringUtils.trim(this.name) + "*"));
        }
        if (Objects.nonNull(this.principalId)) {
            builder.must(QueryBuilders.matchPhraseQuery("principalId", this.principalId));
        }
        if (Objects.nonNull(this.type)) {
            builder.must(QueryBuilders.matchPhraseQuery("type", this.type.name()));
        }
        return builder;
    }
}
